package com.example.zhu.controller.member;

import com.example.zhu.Mapper.MemberMapper;
import com.example.zhu.Mapper.MemberRecordMapper;
import com.example.zhu.VO.MemberVo.MemberVo;
import com.example.zhu.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/member")
@Slf4j
public class MemberController {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    MemberRecordMapper memberRecordMapper ;
    @PostMapping("/page")
    public Reply page(@RequestBody Member member){
        String name = member.getName();
        String phone = member.getMobile();
        MyPage page = member.getPage();
        page.setMin((page.getPage()-1)*page.getSize());
        page.setMax((page.getPage())*page.getSize());
        List<Member> MemberAllList;
        if(name!=null && name.length()>0){
            name="%"+name.trim()+"%";
            MemberAllList = memberMapper.getNameList(name);
        }else if(phone!=null&&phone.length()>0){
            phone="%"+phone.trim()+"%";
            MemberAllList = memberMapper.getPhoneList(phone);
        }else{
            MemberAllList = memberMapper.getAllList(page);
        }
        MemberVo memberVo = new MemberVo();
        memberVo.setRecords(MemberAllList);
        memberVo.setSize(page.getSize()+"");
        memberVo.setCurrent(page.getPage()+"");
        Integer allListCount = Integer.valueOf(memberMapper.getAllListCount());
        memberVo.setTotal(allListCount);
        memberVo.setSearchCount(true);
        memberVo.setPages(String.valueOf(allListCount/page.getSize()+1));
        return Reply.success(memberVo);

    }
    @PostMapping("/modify")
    public Reply<Member>  modify(@RequestBody Member member){
        Member oldMember = memberMapper.selectById(member);
        if (oldMember!=null){
            oldMember.setUpdateTime(new Date());
            oldMember.setName(member.getName());
            oldMember.setMobile(member.getMobile());
            oldMember.setGender(member.getGender());
            oldMember.setUserMoney(member.getUserMoney());
            memberMapper.updataById(oldMember);
            return Reply.success(oldMember);
        }else{
            // "修改失败"
            return Reply.failed(member);
        }
    }
    @PostMapping("/add")
    public Reply<Member>  add(@RequestBody Member member){
        member.setUpdateTime(new Date());
        member.setCreateTime(new Date());
        member.setFrozenMoney(0);
        member.setUserMoney(0);
        memberMapper.Insert(member);
        return Reply.success(member);

    }
    @GetMapping("/delete/{id}")
    public Reply<Member>  delete(@PathVariable(name = "id") String id){
        Member member = new Member();
        member.setId(id);
        Member oldMember = memberMapper.selectById(member);
        if (oldMember!=null){
            memberMapper.Delect(oldMember);
            return Reply.success(member);
        }else{
            // "修改失败"
            return Reply.failed(member);
        }
    }
    @PostMapping("/addMember")
    public Reply<Member>  addMember(@RequestBody Member member){
        Member oldMember = memberMapper.selectById(member);
        if (oldMember!=null){
            oldMember.setUpdateTime(new Date());
            MemberRecord memberRecord = new MemberRecord();
            memberRecord.setMemberId(oldMember.getId());
            memberRecord.setMoneyType("+"+(member.getZhuMoney()+member.getGiveMoney())+"元");
            memberRecord.setMoneyBefore(oldMember.getUserMoney());
            oldMember.setUserMoney(oldMember.getUserMoney() + member.getZhuMoney()+member.getGiveMoney());
            memberRecord.setMoneyAfter(oldMember.getUserMoney());
            memberRecord.setMoneyDate(new Date());
            memberRecord.setType("充值");
            memberRecord.setOrderId("");
            memberRecord.setGiveMoney(member.getGiveMoney());
            memberRecordMapper.Insert(memberRecord);
            memberMapper.updataById(oldMember);
            return Reply.success(oldMember);
        }else{
            // "修改失败"
            return Reply.failed(oldMember);
        }
    }
    @PostMapping("/ReduceMember")
    public Reply<Member>  ReduceMember(@RequestBody Member member){
        Member oldMember = memberMapper.selectById(member);
        if (oldMember!=null){
            oldMember.setUserMoney(oldMember.getUserMoney() - member.getZhuMoney());
            oldMember.setFrozenMoney(oldMember.getFrozenMoney()+member.getZhuMoney());
            oldMember.setUpdateTime(new Date());
            memberMapper.updataById(oldMember);
            return Reply.success(oldMember);
        }else{
            // "修改失败"
            return Reply.failed(oldMember);
        }
    }
    @PostMapping("/selectId")
    public Reply<List<Member>>  selectId(@RequestBody Member member){
        String mobile = member.getMobile();
        member.setMobile("%"+mobile+"%");
        List<Member> members = memberMapper.selectByMember(member);
        return Reply.success(members);
    }

}
